
import org.springframework.beans.factory.annotation.Autowired

import org.springframework.boot.context.properties.ConfigurationProperties

import org.springframework.http.HttpStatus
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.*

@Controller
@RequestMapping("/")
@ConfigurationProperties("amazon")
class ReadingListController{

	@Autowired
	AmazonProperties amazonProperties

	@ExceptionHandler(value=RuntimeException.class)
	@ResponseStatus(value=HttpStatus.BANDWIDTH_LIMIT_EXCEEDED)
	def error(){
		"error"
	}


	@RequestMapping(method=RequestMethod.GET) 
	def readersBooks(Reader reader, Model model) { 
		List<Book> readingList = Book.findAllByReader(reader) 
		model.addAttribute("reader", reader) 
		if (readingList) { 
		model.addAttribute("books", readingList) 
		model.addAttribute("amazonID", amazonProperties.getAssociateId()) 
		} 
		"readingList" 
	} 
	@RequestMapping(method=RequestMethod.POST) 
	def addToReadingList(Reader reader, Book book) { 
		Book.withTransaction { 
		book.setReader(reader) 
		book.save() 
		} 
		"redirect:/" 
	}

}

